package com.melichercik.code;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Align;
import android.widget.EditText;
import android.widget.LinearLayout;

public class IfBrick extends Brick {
	
	public String condition = "";
	
	public Brick then;
	public Brick endthen;
	public Brick els;
	public Brick endels;
	public Brick endif;
	public int leftWidth = 0;
	public int rightWidth = 0;

	public IfBrick() {
		super(Brick.IF);
	}
	
	public IfBrick(Activity activity) {
		super(IF);
		createDialog(activity);
	}
	
	public void edit(Activity activity) {
		createDialog(activity);
	}
	
	public synchronized void createDialog(Activity activity) {
		
		AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
		
		dialog.setTitle(getCaption(type));
		
		dialog.setMessage("Define condition.");
		
		LinearLayout lila= new LinearLayout(activity);
		lila.setOrientation(LinearLayout.VERTICAL);
		
		final EditText input = new EditText(activity);
		input.setHint("condition");
		lila.addView(input);
		
		dialog.setView(lila);
		
		dialog.setPositiveButton("OK",  new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
				if (!input.getText().toString().isEmpty()) {
					condition = input.getText().toString();
				} else {
					condition = "1 = 1";
				}
			}
		});
		
		dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		dialog.show();
		
		input.setText(condition);
	}
	
	public synchronized void render(Canvas canvas, int offsetX, int offsetY, int scale) {
		
		Paint myPaint = new Paint();
		
		int realX = this.xx = (x * scale) + (x * scale / 2) + offsetX;
		int realY = this.yy = (y * scale) + (y * scale / 2) + offsetY;
		this.scale = scale;
		
		myPaint.setColor(Color.rgb(100, 100, 100));
		int lineSize = scale / 20;
		
		int x1 = realX - Math.abs(x - then.x) * (scale + scale / 2) + scale / 2 - lineSize;
		int x2 = realX + Math.abs(x - els.x) * (scale + scale / 2) + scale / 2  + lineSize;
		int y1 = realY + scale / 2 - lineSize;
		int y2 = realY + Math.abs(y - endif.y) * (scale + scale / 2) + scale / 2 + lineSize;
		
		canvas.drawRect(x1, y1, x2, y2,	myPaint);
		
		myPaint.setColor(Color.rgb(255, 255, 255));
		canvas.drawRect(x1 + lineSize * 2, y1 + lineSize * 2, x2 - lineSize * 2, y2 - lineSize * 2, myPaint);
		
		myPaint.setColor(Color.rgb(251, 154, 62));
		RectF rect = new RectF(realX, realY, realX + scale, realY + scale);
		canvas.drawRoundRect(rect, (float) scale / 10,(float) scale / 10, myPaint);
		
		int lineHeight = (int) (40 * ((float) scale / 200));
		
		int realCaptionX = (x * scale) + (scale / 2) + (x * scale / 2) + offsetX;
		int realCaptionY = (int) ((y * scale) + ((scale / 2) * 1.1) + (y * scale / 2) + offsetY - ((float) lineHeight * 3 / 2));
		
		int captioFontSize = (int) (40 * ((float) scale / 200));
		
		myPaint.setColor(Color.rgb(255, 255, 255));
		myPaint.setTextSize(captioFontSize);
		myPaint.setTextAlign(Align.CENTER);
		canvas.drawText(getCaption(), realCaptionX, realCaptionY, myPaint);
		
		renderVariables(canvas, realX, realY, scale, lineHeight);
	}
	
	private void renderVariables(Canvas canvas, int x, int y, int scale, int height) {
		
		Paint myPaint = new Paint();
		myPaint.setTextAlign(Align.CENTER);
		
		int captioFontSize = (int) (35 * ((float) scale / 200));
		
		int realCaptionX = x + (scale / 2);
		int realCaptionY = y + (scale / 2) + (int) (captioFontSize * 1.3);
		
		myPaint.setColor(Color.rgb(255, 255, 255));
		myPaint.setTextSize(captioFontSize);
		
		canvas.drawText(textTruncate(condition), realCaptionX, realCaptionY - (int) (captioFontSize), myPaint);
	}
}
